using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using MCromwell.StaffIntranet.Presenters;

namespace MCromwell.StaffIntranet.UI
{
    public partial class Login : Page, IAuthenticationView
    {
        private AuthenticationPresenter authPresenter;
        private CommonPresenter commonPresenter;
        public event LoginPerformedDelegate LoginPerformed;

        protected void Page_Load(object sender, EventArgs e)
        {
            BindViewEvents();
            InitializePresenters();
        }

        private void InitializePresenters()
        {
            authPresenter = new AuthenticationPresenter(this);
            commonPresenter = new CommonPresenter((ICommonView)Master);
        }

        private void BindViewEvents()
        {
 	        btnLogin.Click += delegate 
            {
                RaiseLoginPerformedEvent();
            };
        }

        private void RaiseLoginPerformedEvent()
        {
            LoginPerformedEventArgs e = new LoginPerformedEventArgs(txtUsername.Text, txtPassword.Text);
            if (LoginPerformed != null)
                LoginPerformed(this, e);
        }

        public void CredentialsAuthenticated(Guid loginSessionId)
        {
            lblMessage.Text = "Logged in successfully";
            LoginManager.AddLogin(loginSessionId);
            commonPresenter.Initialize(loginSessionId);
        }

        public void DisplayError(string message)
        {
            lblMessage.Text = message;
        }
                
    }
}
